﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Entities;

namespace AtletIS.Shared.Forms
{
    public partial class frmPreChipData : Form
    {
        private DisciplineListEntity discipline;

        // Constructor
        public frmPreChipData(DisciplineListEntity discipline)
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;

            this.discipline = discipline;
            this.lblTitle.Text = discipline.Name;
        }

        // Form load
        private void frmPreChipData_Load(object sender, EventArgs e)
        {
            // Start, calc time
            if (this.discipline.RealStartTime.HasValue)
            {
                this.dtpRealStartTime.Value = this.discipline.RealStartTime.Value;
                this.dtpCalcTime.Value = this.discipline.RealStartTime.Value;
            }
            else
            {
                MessageBox.Show("Nebol zadaný skutočný čas štartu!", "Prechody pred štartom");
            }

            // Fill table
            DatabaseHelper.FillPreChipData(this.aDbDataSet.PreChipData, this.discipline.ID);

            this.chkTime_CheckedChanged(null, null);
        }

        // Save changes
        private void tlsSave_Click(object sender, EventArgs e)
        {
            if (this.aDbDataSet.PreChipData != null && this.aDbDataSet.PreChipData.Rows.Count > 0)
            {
                foreach (DataRow dr in this.aDbDataSet.PreChipData.Rows)
                {
                    if (dr.RowState != DataRowState.Deleted)
                    {
                        dr["CompetitionId"] = discipline.CompetitionId;
                        dr["MeasurementId"] = discipline.MeasurementId;
                    }
                }
            }

            this.Validate();
            this.preChipDataBindingSource.EndEdit();
            this.preChipDataTableAdapter.Update(this.aDbDataSet.PreChipData);
        }

        // Calc
        private void btnCalc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Naozaj chcete pregenerovať prechody?", "Prechody pred štartom", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;

            try
            {
                DateTime? calcTime = null;
                if (this.chkTime.Checked)
                    calcTime = this.dtpCalcTime.Value;

                DatabaseHelper.UpdatePreChipData(this.discipline.ID, calcTime);

                // Re-fill table
                DatabaseHelper.FillPreChipData(this.aDbDataSet.PreChipData, this.discipline.ID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nastala chyba: " + ex.Message, "Prechody pred štartom", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        // Close
        private void tlsClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkTime_CheckedChanged(object sender, EventArgs e)
        {
            this.dtpCalcTime.Enabled = chkTime.Checked;
        }

        private void txtStartTime_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

    }
}
